package belle.com.myapp.view.home.activity;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.widget.ImageView;

import com.bilibili.magicasakura.widgets.TintToolbar;

import java.util.ArrayList;
import java.util.List;

import belle.com.myapp.R;
import belle.com.myapp.base.BaseActivity;
import belle.com.myapp.baseview.CustomViewPager;
import belle.com.myapp.view.home.fragment.MainFragment;
import belle.com.myapp.view.home.fragment.RecommendFragment;
import belle.com.myapp.view.home.fragment.VideoFragment;
import butterknife.BindView;

/**
 * Title :主页
 * Description :
 * Author:  Belle Guan    date : 2018/5/16 15:40
 * Update :                date :
 * Version : 1.0.0
 * Copyright : copyright(c) 浙江亚厦股份有限公司 2017 ~ 2020 版权所有
 *
 * @author guanjiali1
 */
public class HomeActivity extends BaseActivity {
    @BindView(R.id.bar_net)
    ImageView barNet;
    @BindView(R.id.bar_music)
    ImageView barMusic;
    @BindView(R.id.bar_friends)
    ImageView barFriends;
    @BindView(R.id.toolbar)
    TintToolbar toolbar;
    @BindView(R.id.view_pager)
    CustomViewPager viewPager;
    private ActionBar ab;
    private ArrayList<ImageView> tabs = new ArrayList<>();

    @Override
    protected int isTranslucentStatus() {
        return 0;
    }

    @Override
    protected boolean isNeedEventBus() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setToolBar();
        setViewPager();
    }

    private void setToolBar() {
        setSupportActionBar(toolbar);
        ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setHomeAsUpIndicator(R.drawable.ic_menu);
        ab.setTitle("");

    }

    private void setViewPager() {
        tabs.add(barNet);
        tabs.add(barMusic);
        tabs.add(barFriends);

        MainFragment mainFragment = new MainFragment();
        RecommendFragment recommendFragment = new RecommendFragment();
        VideoFragment videoFragment = new VideoFragment();

        CustomViewPagerAdapter customViewPager = new CustomViewPagerAdapter(getSupportFragmentManager());
        customViewPager.addFragment(mainFragment);
        customViewPager.addFragment(recommendFragment);
        customViewPager.addFragment(videoFragment);
        viewPager.setAdapter(customViewPager);
        viewPager.setCurrentItem(1);
        barMusic.setSelected(true);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switchTabs(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        barNet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);
            }
        });
        barMusic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1);
            }
        });
        barFriends.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(2);
            }
        });
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    private void switchTabs(int position) {
        for (int i = 0; i < tabs.size(); i++) {
            if (position == i) {
                tabs.get(i).setSelected(true);
            } else {
                tabs.get(i).setSelected(false);
            }
        }
    }

    static class CustomViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();

        public CustomViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment) {
            mFragments.add(fragment);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

    }

}
